package distributelock

import (
	"fmt"
	r "lnzw/engine/redis"
	"time"
)

type redisLock struct {
}

func (lock *redisLock) Init() error {
	if err := r.Setup(); err != nil {
		return err
	}
	return nil
}

func (lock *redisLock) Lock(key string) (bool, error) {
	keymapping := fmt.Sprintf("%s%s", lock_prefix, key)
	rs, err := r.SetNxEx(keymapping, "1", 5*time.Second)
	if err != nil {
		return false, err
	}
	return rs, nil
}

func (lock *redisLock) UnLock(key string) (bool, error) {
	keymapping := fmt.Sprintf("%s%s", lock_prefix, key)
	return r.Del(keymapping)
}
